activity3.ipynb
No Headings
The table of contents shows headings in notebooks and supported files.
- File
- Edit
- View
- Run
- Kernel
- Settings
- Help
Kernel status: Idle Executed 2 cellsElapsed time: 2 seconds
PassengerId Survived Pclass \
0 1 No 3
1 2 Yes 1
2 3 Yes 3
3 4 Yes 1
4 5 No 3
Name Sex Age SibSp \
0 Braund, Mr. Owen Harris male 22.0 1
1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1
2 Heikkinen, Miss. Laina female 26.0 0
3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1
4 Allen, Mr. William Henry male 35.0 0
Parch Ticket Fare Cabin Embarked
0 0 A/5 21171 7.2500 NaN S
1 0 PC 17599 71.2833 C85 C
2 0 STON/O2. 3101282 7.9250 NaN S
3 0 113803 53.1000 C123 S
4 0 373450 8.0500 NaN S
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 PassengerId 891 non-null int64
1 Survived 891 non-null object
2 Pclass 891 non-null int64
3 Name 891 non-null object
4 Sex 891 non-null object
5 Age 714 non-null float64
6 SibSp 891 non-null int64
7 Parch 891 non-null int64
8 Ticket 891 non-null object
9 Fare 891 non-null float64
10 Cabin 204 non-null object
11 Embarked 889 non-null object
dtypes: float64(2), int64(4), object(6)
memory usage: 83.7+ KB
None
PassengerId 0
Survived 0
Pclass 0
Name 0
Sex 0
Age 177
SibSp 0
Parch 0
Ticket 0
Fare 0
Cabin 687
Embarked 2
dtype: int64
[66]:
Selection deleted
# Extract the title from the Name column
df["Title"] = df["Name"].str.extract(r",\s*([^\.]+)\.")
# Display unique titles
print("Unique Titles:", df["Title"].unique())
# Define high-ranking titles
high_rank_titles = ["Capt", "Col", "Major", "Dr", "Rev", "Sir", "Lady", "Don", "Dona", "Jonkheer", "the Countess"]
# Create a new column indicating whether a passenger had a rank (True/False)
df["Rank"] = df["Title"].isin(high_rank_titles)
# Display the first few rows with new columns
df[["Name", "Title", "Rank"]].head()
Unique Titles: ['Mr' 'Mrs' 'Miss' 'Master' 'Don' 'Rev' 'Dr' 'Mme' 'Ms' 'Major' 'Lady' 'Sir' 'Mlle' 'Col' 'Capt' 'the Countess' 'Jonkheer']
[66]:
| Name | Title | Rank | |
|---|---|---|---|
| 0 | Braund, Mr. Owen Harris | Mr | False |
| 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | Mrs | False |
| 2 | Heikkinen, Miss. Laina | Miss | False |
| 3 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | Mrs | False |
| 4 | Allen, Mr. William Henry | Mr | False |
[2]:
# Visualization 1: Survival rate by Title
plt.figure(figsize=(12, 6))
sns.countplot(data=df, x="Title", hue="Survived", palette="coolwarm")
plt.xticks(rotation=45)
plt.title("Survival Rate by Title")
plt.show()
# Visualization 2: Survival rate by Rank (Boolean Feature)
plt.figure(figsize=(8, 6))
sns.countplot(data=df, x="Rank", hue="Survived", palette="coolwarm")
plt.title("Survival Rate by Rank")
plt.show()
# Visualization 3: Survival Rate by Pclass and Title
plt.figure(figsize=(14, 6))
sns.catplot(data=df, x="Pclass", hue="Survived", col="Title", kind="count", col_wrap=4, palette="coolwarm")
plt.show()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[2], line 2 1 # Visualization 1: Survival rate by Title ----> 2 plt.figure(figsize=(12, 6)) 3 sns.countplot(data=df, x="Title", hue="Survived", palette="coolwarm") 4 plt.xticks(rotation=45) NameError: name 'plt' is not defined
[ ]:
Common Tools
No metadata.
Advanced Tools
No metadata.
Anaconda Assistant
AI-powered coding, insights and debugging in your notebooks.
To enable the following extensions, create an account or sign in.
- Anaconda Assistant4.1.0
- Coming soon!
- Data Catalogs
- Panel Deployments
- Sharing
Already have an account? Sign In
For more information, read our Anaconda Assistant documentation.
![Python [conda env:base] *](./activity3_files/logo-64x64.png)